﻿module Ast

open Microsoft.FSharp.Text.Lexing

type Node<'a> = { Node: 'a; Start: Position; End: Position }

type value =
    | Int of Node<int>
    | Float of Node<float>
    | String of Node<string>
 
type direction =
    | Asc = 0
    | Desc = 1

type op =
    | Eq = 0
    | Gt = 1
    | Ge = 2
    | Lt = 3
    | Le = 4

type orderA = { Order : string; Direction : direction }
type order = Node<orderA>

type groupA = { Group : string }
type group = Node<groupA>

type whereA =
    | Cond of (value * op * value)
    | And of Node<whereA> * Node<whereA>
    | Or of Node<whereA> * Node<whereA>
and where = Node<whereA>

type joinType = 
    | Inner = 0
    | Left = 1
    | Right = 2

type sqlStatement =
    { Columns : column list;
      From : from
      Where : where option;
      OrderBy : order list;
      GroupBy : group list }

and subQuery = 
    { query : sqlStatement;
      alias : string }

and from =
   { Table : table;
     Joins : join list; }

and column =
    | Function of functionCall
    | Field of string
    | SubQueryc of subQuery

and functionCall =
    string * column list

and table = 
    | Table of Node<string>
    | SubQueryt of Node<subQuery>

and joinA = {
    Type : joinType;
    Table : table;
    On : where option }

and join = Node<joinA>

